package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	s := "Yes我爱慕课网!" //UTF-8
	fmt.Println(s)
	//fmt.Printf("%X\n", []byte(s))

	//我: E6 88 91
	for _, b := range []byte(s) {
		//fmt.Print(b)
		fmt.Printf("%X ", b) //==>16进制
	}
	fmt.Println()

	// ch is a rune ==> unicode
	// 我：6211
	for i, ch := range s {
		fmt.Printf("(%d %X)", i, ch)
	}
	fmt.Println()

	//获得字符数。len获得的是字节数
	fmt.Println("Rune count: ", utf8.RuneCountInString(s))

	bytes := []byte(s)
	fmt.Printf("len(bytes)=%d, bytes=%v \n", len(bytes), bytes)
	for len(bytes) > 0 {
		ch, size := utf8.DecodeRune(bytes)
		bytes = bytes[size:]
		fmt.Printf("%c ", ch)
	}
	fmt.Println()

	//使用rune,每个ch占4个字节
	for i, ch := range []rune(s) {
		//fmt.Print(b)
		fmt.Printf("(%d %c)", i, ch) //%c==>unicode比那吗
	}
	fmt.Println()

}
